namespace cnVery.Cms.Ctrl
{
    using cnVery.Data;
    using cnVery.Utils;
    using System;
    using System.Web.UI.WebControls;

    public class Rep : Repeater
    {
        private string _category;
        private int _count;
        private string _fields;
        private string _sort;
        private string _sql;
        private string _table;
        private string _where;

        private void InitializeComponent()
        {
            base.Load += new EventHandler(this.Page_Load);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if ((this._sql != null) && (this._sql != string.Empty))
            {
                try
                {
                    Connection connection = new Connection();
                    this.DataSource = connection.GetDataTable(this._sql);
                    this.DataBind();
                    connection.Close();
                    return;
                }
                catch (Exception exception)
                {
                    new Terminator().Throw(exception.Message + this._sql);
                }
            }
            string str = (this._count > 0) ? (" top " + this._count.ToString()) : " ";
            string str2 = ((this._fields == null) || (this._fields == string.Empty)) ? " * " : this._fields;
            string str3 = ((this._where == null) || (this._where == string.Empty)) ? " " : (" where " + this._where);
            if ((this._category != null) && (this._category != string.Empty))
            {
                string str4 = "categoryid in(SELECT id FROM categories WHERE (id = (SELECT TOP 1 id FROM categories WHERE (name = '$CAT'))) OR (parentid = (SELECT TOP 1 id FROM categories WHERE (name = '$CAT'))))".Replace("$CAT", this._category);
                if (str3 == " ")
                {
                    str3 = "where " + str4;
                }
                str3 = str3 + "and " + str4;
            }
            string str5 = ((this._sort == null) || (this._sort == string.Empty)) ? " " : (" order by " + this._sort);
            string sql = string.Format("select {0} {1} from {2} {3} {4}", new object[] { str, str2, this._table, str3, str5 });
            try
            {
                Connection connection2 = new Connection();
                this.DataSource = connection2.GetDataTable(sql);
                this.DataBind();
                connection2.Close();
            }
            catch (Exception exception2)
            {
                new Terminator().Throw(exception2.Message + sql);
            }
        }

        public string Category
        {
            get
            {
                return this._category;
            }
            set
            {
                this._category = value;
            }
        }

        public int Count
        {
            get
            {
                return this._count;
            }
            set
            {
                this._count = value;
            }
        }

        public string Fields
        {
            get
            {
                return this._fields;
            }
            set
            {
                this._fields = value;
            }
        }

        public string Sort
        {
            get
            {
                return this._sort;
            }
            set
            {
                this._sort = value;
            }
        }

        public string Sql
        {
            get
            {
                return this._sql;
            }
            set
            {
                this._sql = value;
            }
        }

        public string Table
        {
            get
            {
                return this._table;
            }
            set
            {
                this._table = value;
            }
        }

        public string Where
        {
            get
            {
                return this._where;
            }
            set
            {
                this._where = value;
            }
        }
    }
}

